package ldkxingzhe.l365calendar.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.activity.Login;
import ldkxingzhe.l365calendar.view.VerticalViewPager;

/**
 * 引导页
 * Created by ke on 16-1-16.
 */
public class GuideFragment extends Fragment{
    @SuppressWarnings("unused")
    private final String TAG = "GuideFragment";

    private TextView versionText;
    private VerticalViewPager verticalViewPager;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View resultView = inflater.inflate(R.layout.guide_vp, container, false);
        versionText = (TextView) resultView.findViewById(R.id.version);
        verticalViewPager = (VerticalViewPager) resultView.findViewById(R.id.vertical_view_Pager);
        verticalViewPager.setAdapter(new VerticalPagerAdapter());
        return resultView;
    }

    class VerticalPagerAdapter extends PagerAdapter{

        private final int[] images = {R.drawable.guide_img1, R.drawable.guide_img2, R.drawable.guide_img3};
        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View resultView = LayoutInflater.from(container.getContext()).inflate(R.layout.guide_item_layout, null);
            ImageView backImage = (ImageView) resultView.findViewById(R.id.image);
            ImageView btnEnterBg = (ImageView) resultView.findViewById(R.id.btn_enter_bg);
            View btnEnterLayout = resultView.findViewById(R.id.btn_enter_layout);
            ImageView iconNext = (ImageView) resultView.findViewById(R.id.iv_next_icon);

            backImage.setImageResource(images[position]);
            switch (position){
                case 0:
                case 1:
                    iconNext.setVisibility(View.VISIBLE);
                    btnEnterLayout.setVisibility(View.GONE);
                    iconNext.setAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.next_alpha_anim));
                    break;
                case 2:
                    iconNext.setVisibility(View.GONE);
                    btnEnterLayout.setVisibility(View.VISIBLE);
                    btnEnterBg.setAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.guide_enter_btn_anim));
                    btnEnterLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ((Login)getActivity()).startFromGuide();
                        }
                    });
                    break;
                default:
                    Log.e(TAG,"ViewPager, and position is wrong");
            }
            container.addView(resultView);
            return resultView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}
